package com.cidp.system.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.extremecomponents.table.limit.Limit;

import com.cidp.system.model.Tmenu;
import com.cidp.system.pojo.MenuPo;
import com.cidp.system.service.IMenuService;
import com.litt.core.common.Constants;
import com.litt.core.page.IPageList;
import com.litt.core.page.PagePoParam;
import com.litt.core.util.ExtremeTableUtil;

/**
 * <b>标题：</b>系统菜单.
 * <pre><b>描述</b>
 *    系统菜单管理
 * </pre>
 * 
 * @author lijian
 * @since 2008-6-3
 * @version 1.0
 */
public class MenuAction extends DispatchAction{
	
	private IMenuService menuService;
	
	/**
	 * 查看系统菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward search(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		MenuPo menuPo = (MenuPo)dynaActionForm.get("menuPo");
		
		Limit limit=ExtremeTableUtil.getLimit(request,Constants.DEFAULT_PAGE_SIZE);
		IPageList page = menuService.listByPage(new PagePoParam(menuPo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		return mapping.findForward("toList");
	}

	/**
	 * 保存系统菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward save(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		MenuPo menuPo = (MenuPo)dynaActionForm.get("menuPo");
		
		Tmenu menu = new Tmenu();
		menu.setIsLeaf(new Long(1));
		menu.setMenuCode(menuPo.getMenuCode());
		menu.setMenuName(menuPo.getMenuName());
		menu.setMenuUrl(menuPo.getMenuUrl());
		//父节点的问题
		menu.setParentId(menuPo.getParentId());
		menu.setPosition(menuPo.getPosition());
		menu.setRemark(menuPo.getRemark());
		menu.setStatus(menuPo.getStatus());
		
		menuService.saveTmenu(menu);
		return mapping.findForward("toSave");
	}
	
	/**
	 * 新增系统菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		return mapping.findForward("toAdd");
	}
	
	/**
	 * 修改系统菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward edit(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		MenuPo menuPo = (MenuPo)dynaActionForm.get("menuPo");
		
		String menuId = request.getParameter("menuId");
		Tmenu menu = menuService.loadById(new Long(menuId));
		try{
			BeanUtils.copyProperties(menuPo, menu);
		}catch(Exception e){
			
		}
		dynaActionForm.set("menuPo", menuPo);
		return mapping.findForward("toEdit");
	}
	
	/**
	 * 更新系统菜单
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		MenuPo menuPo = (MenuPo)dynaActionForm.get("menuPo");
		
		Tmenu menu = menuService.loadById(menuPo.getMenuId());
		menu.setIsLeaf(new Long(1));
		menu.setMenuCode(menuPo.getMenuCode());
		menu.setMenuName(menuPo.getMenuName());
		menu.setMenuUrl(menuPo.getMenuUrl());
		//父节点的问题
		menu.setParentId(menuPo.getParentId());
		menu.setPosition(menuPo.getPosition());
		menu.setRemark(menuPo.getRemark());
		menu.setStatus(menuPo.getStatus());
		
		menuService.updateTmenu(menu);
		
		return mapping.findForward("toUpdate");
	}

	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}
}
